package tw.com.softleader.fire.setting.web;

import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import tw.com.softleader.fire.setting.entity.AcuAddrEntity;
import tw.com.softleader.fire.setting.entity.RoadCodeEntity;
import tw.com.softleader.fire.setting.service.RoadCodeService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
/**
 Author:Billy
 */
public class RoadCodeAction extends ActionSupport {

	@Autowired
	private RoadCodeService roadCodeService;
	private RoadCodeEntity model;
	private List<RoadCodeEntity> models;
	private String action;
	private String searchBox;
	private String search;
	
	
	public String getSearch() {
		return search;
	}
	public void setSearch(String search) {
		this.search = search;
	}
	public String getSearchBox() {
		return searchBox;
	}
	public void setSearchBox(String searchBox) {
		this.searchBox = searchBox;
	}
	
	
	public RoadCodeEntity getModel() {
		return model;
	}

	public void setModel(RoadCodeEntity model) {
		this.model = model;
	}

	public List<RoadCodeEntity> getModels() {
		return models;
	}
	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	private Logger log = LoggerFactory.getLogger(RoadCodeAction.class);
	
	
	@Override
	public void validate() {
	}

	public String execute() throws Exception {
		log.debug("execute RoadCode");
		if(search!=null && search.equals("ADDR_CODE")){
			log.debug("execute search by ADDR_CODE");
			log.debug("Model = {}", model);
			
			model = new RoadCodeEntity();
			model.setAddrCode(this.searchBox);
			models = roadCodeService.searchByAddCode(model);
			
			log.debug("Models = {}", models);
			
			return SUCCESS;
		}else if(search!=null && search.equals("ROAD_CODE")){
			log.debug("execute search by ROAD_CODE");
			log.debug("Model = {}", model);
			
			model = new RoadCodeEntity();
			model.setRoadCode(this.searchBox);
			models = roadCodeService.searchByRoadCode(model);
			
			log.debug("Models = {}", models);
			
			return SUCCESS;
		}else if(search!=null && search.equals("NAME")){
			log.debug("execute search by Name");
			log.debug("Model = {}", model);
			
			model = new RoadCodeEntity();
			model.setRoadName(this.searchBox);
			models = roadCodeService.searchByName(model);
			
			log.debug("Models = {}", models);
			
			return SUCCESS;
		}
		
		
		
		//System.out.println("action="+action);
		if (action != null && action.equals("刪除")) {
			log.debug("delete data");
			log.debug("Model = {}", model);
			try {
				roadCodeService.delete(model);
			} catch (Exception e) {
				log.debug("delete data failed");
			}

		} else if (action != null && action.equals("修改")) {
			log.debug("update data");
			model.setModifier("Billy");
			model.setModifiedTime(LocalDateTime.now());
			log.debug("Model ={}",model);
			try {
				roadCodeService.update(model);
			} catch (Exception e) {
				log.debug("update data failed");
			}
		}else if(action != null && action.equals("insert")) {
			log.debug("insert data");
			model.setId(null);
			model.setModifier(null);
			model.setModifiedTime(null);
			model.setCreator("Billy");
			model.setCreateTime(LocalDateTime.now());
			log.debug("Model ={}",model);
			try {
				roadCodeService.insert(model);
			} catch (Exception e) {
				log.debug("insert data failed");
				addFieldError("Insert Fail","There is something wrong.");
			}
		}
		
		try {
			models = roadCodeService.getAll();
			log.debug("Models = {}", models);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Action.SUCCESS;
	}
	
}